package com.example.demomybatis.controller;


import cn.hutool.db.PageResult;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demomybatis.entity.TicketModelType;
import com.example.demomybatis.model.TicketModelTypeVo;
import com.example.demomybatis.model.TicketQuery;
import com.example.demomybatis.service.ITicketModelTypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 模型名称和服务类型关系表 前端控制器
 * </p>
 *
 * @author zhaoheng
 * @since 2023-08-07
 */
@RestController
@RequestMapping("/ticket-model-type")
@Api(tags = "工单模型")
@Slf4j
public class TicketModelTypeController {
    @Resource
    ITicketModelTypeService iTicketModelTypeService;

    @ApiOperation("查询全部")
    @GetMapping("list")
    public List<TicketModelType> list() {
        return iTicketModelTypeService.list();
    }

    @ApiOperation("分页查询")
    @GetMapping("page")
    public PageInfo<TicketModelType> page(@RequestParam(defaultValue = "1", required = false) @ApiParam( value = "页码", defaultValue = "1") Integer pageNum,
                                      @RequestParam(defaultValue = "4", required = false) @ApiParam(value = "条数", defaultValue = "5") Integer pageSize) {
        log.info("请求参数：pageNum:{},pageSize:{}", pageNum, pageSize);
        PageHelper.startPage(pageNum,pageSize);
        LambdaQueryWrapper<TicketModelType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.ne(TicketModelType::getServiceType, "网络安全");
        lambdaQueryWrapper.orderByDesc(TicketModelType::getId);
        List<TicketModelType> typeList = iTicketModelTypeService.list(lambdaQueryWrapper);
        PageInfo<TicketModelType> pageInfo = new PageInfo<>(typeList);
        return pageInfo;
    }

    @ApiOperation("分页查询2")
    @GetMapping("page2")
    public PageResult<TicketModelType> page2(@RequestParam(defaultValue = "1", required = false) @ApiParam( value = "页码", defaultValue = "1") Integer pageNum,
                            @RequestParam(defaultValue = "4", required = false) @ApiParam(value = "条数", defaultValue = "5") Integer pageSize) {
        log.info("请求参数：pageNum:{},pageSize:{}", pageNum, pageSize);
        PageHelper.startPage(pageNum,pageSize);
        LambdaQueryWrapper<TicketModelType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.ne(TicketModelType::getServiceType, "网络安全");
        lambdaQueryWrapper.orderByDesc(TicketModelType::getId);
        List<TicketModelType> typeList = iTicketModelTypeService.list(lambdaQueryWrapper);
        PageInfo<TicketModelType> pageInfo = new PageInfo<>(typeList);
        PageResult<TicketModelType> pageResult = new PageResult<>();
        pageResult.addAll(pageInfo.getList());
        pageResult.setTotal((int) pageInfo.getTotal());
        pageResult.setPage(pageInfo.getPageNum());
        pageResult.setTotalPage(pageInfo.getPages());
        pageResult.setPageSize(pageInfo.getPageSize());
        return pageResult;
    }

    @ApiOperation("复杂查询")
    @GetMapping("complexQuery")
    public List<TicketModelType> complexQuery() {
        return iTicketModelTypeService.complexQuery();
    }

    @ApiOperation("执行原生SQL")
    @GetMapping("nativeSql")
    public List<TicketModelType> nativeSql() {
        return iTicketModelTypeService.sqlQuery();
    }

    @ApiOperation("条件查询")
    @GetMapping("queryByCondition")
    public List<TicketModelType> queryByCondition(TicketQuery ticketQuery) {
        log.info("请求参数：{}", JSON.toJSONString(ticketQuery));
        return iTicketModelTypeService.queryByCondition(ticketQuery);
    }

    @ApiOperation("条件查询VO")
    @GetMapping("queryVoByCondition")
    public List<TicketModelTypeVo> queryVoByCondition(TicketQuery ticketQuery) {
        log.info("请求参数：{}", JSON.toJSONString(ticketQuery));
        return iTicketModelTypeService.queryVoByCondition(ticketQuery);
    }

    @ApiOperation("拼接SQL片段")
    @GetMapping("jointSql")
    public List<TicketModelType> jointSql() {
        return iTicketModelTypeService.jointSql();
    }


}
